<?php
/**
 * 菜单管理
 * @author zzdboy
 *
 */

require_once ROOT_DIR . 'Library' . DIRECTORY_SEPARATOR . 'Cms' . DIRECTORY_SEPARATOR . 'Tree.class.php';

class MenuAction extends AdminAction {
	
	public function index() {
		
		$tree = new Cms_Tree ();
		$tree->icon = array (
				'&nbsp;&nbsp;&nbsp;│ ',
				'&nbsp;&nbsp;&nbsp;├─ ',
				'&nbsp;&nbsp;&nbsp;└─ ' 
		);
		$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		
		$Menu = D ( "Menu" );
		// 导入分页类
		import ( "ORG.Util.Page" );
		$count = $Menu->where ()->count ();
		$Page = new Page ( $count, 100 );
		$show = $Page->show ();
		$result = $Menu->order ( "listorder ASC,id ASC" )->limit ( $Page->firstRow . ',' . $Page->listRows )->select ();
		
		$array = array ();
		foreach ( $result as $r ) {
			$r ['str_manage'] = '<a href="/menu/add/parentid/' . $r ['id'] . '/">添加子菜单</a> | ';
			$r ['str_manage'] .= '<a href="/menu/edit/id/' . $r ['id'] . '/">修改</a> | ';
			$r ['str_manage'] .= '<a href="javascript:;" onclick="delete_menu(' . $r ['id'] . ')">删除</a>';
			$array [] = $r;
		}
		
		$str = "<tr>
		<td align='center'><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input-text-c'></td>
		<td align='center'>\$id</td>
		<td >\$spacer\$name</td>
		<td align='center'>\$str_manage</td>
		</tr>";
		$tree->init ( $array );
		$categorys = $tree->get_tree ( 0, $str );
		
		$this->assign ( 'categorys', $categorys );
		$this->assign ( 'page', $show );
		$this->display ();
	}
	
	/**
	 * 添加菜单
	 */
	public function add() {
		if ($this->isPost ()) {
			$Menu = D ( "Menu" );
			$data ['name'] = $this->_post ( "name" );
			if (empty ( $data ['name'] )) {
				$this->error ( "菜单名称不能为空!" );
			}
			$data ['parentid'] = $this->_post ( "parentid" );
			if (empty ( $data ['parentid'] )) {
				$this->error ( "上级菜单不能为空!" );
			}
			$data ['controller'] = $this->_post ( "controller" );
			if (empty ( $data ['controller'] )) {
				$this->error ( "功能地址不能为空!" );
			}
			$data ['data'] = $this->_post ( "data" );
			$data ['listorder'] = 0;
			$data ['display'] = 1;
			
			$Menu->create ( $data );
			$return = $Menu->add ();
			
			if ($return) {
				$this->success ( "添加菜单成功!", "/menu/" );
			} else {
				$this->error ( "添加菜单失败!" );
			}
		} else {
			$tree = new Cms_Tree ();
			$Menu = D ( "Menu" );
			$result = $Menu->order ( "listorder ASC,id ASC" )->select ();
			
			$parentid = $this->_get ( 'parentid' );
			
			$array = array ();
			foreach ( $result as $r ) {
				$r ['cname'] = $r ['name'];
				$r ['selected'] = $r ['id'] == $parentid ? 'selected' : '';
				$array [] = $r;
			}
			$str = "<option value='\$id' \$selected>\$spacer \$cname</option>";
			$tree->init ( $array );
			$select_categorys = $tree->get_tree ( 0, $str );
			
			$this->view->assign ( 'select_categorys', $select_categorys );
			$this->display ();
		}
	}
	
	/**
	 * 编辑菜单
	 */
	public function edit() {
		if ($this->isPost ()) {
			$Menu = D ( "Menu" );
			$data ['name'] = $this->_post ( "name" );
			if (empty ( $data ['name'] )) {
				$this->error ( "菜单名称不能为空!" );
			}
			$data ['parentid'] = $this->_post ( "parentid" );
			if (empty ( $data ['parentid'] )) {
				$this->error ( "上级菜单不能为空!" );
			}
			$data ['controller'] = $this->_post ( "controller" );
			if (empty ( $data ['controller'] )) {
				$this->error ( "功能地址不能为空!" );
			}
			$data ['data'] = $this->_post ( "data" );
			$data ['display'] = 1;
			
			$id = $this->_post ( "id" );
			$return = $Menu->where ( "id={$id}" )->data ( $data )->save ();
			
			if ($return) {
				$this->success ( "编辑菜单成功!", "/menu/" );
			} else {
				$this->error ( "编辑菜单失败!" );
			}
		} else {
			$id = $this->_get ( 'id' );
			
			$tree = new Cms_Tree ();
			$Menu = D ( "Menu" );
			$result = $Menu->order ( "listorder ASC,id ASC" )->select ();
			
			$parentid_Info = $Menu->find ( $id );
			$parentid = $parentid_Info ['parentid'];
			
			$array = array ();
			foreach ( $result as $r ) {
				$r ['cname'] = $r ['name'];
				$r ['selected'] = $r ['id'] == $parentid ? 'selected' : '';
				$array [] = $r;
			}
			$str = "<option value='\$id' \$selected>\$spacer \$cname</option>";
			$tree->init ( $array );
			$select_categorys = $tree->get_tree ( 0, $str );
			
			$Menu_info = $Menu->find ( $id );
			$this->assign ( 'Menu_info', $Menu_info );
			$this->assign ( 'select_categorys', $select_categorys );
			$this->assign ( 'id', $id );
			$this->display ();
		}
	}
	
	/**
	 * 删除菜单
	 */
	function delete() {
		$id = $this->_post ( 'id' );
		if (empty ( $id )) {
			$msg = array (
					'rtn_code' => - 1,
					'content' => '参数不能为空,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
		
		$Menu = D ( "Menu" );
		$return = $Menu->where ( "id={$id}" )->delete ();
		
		if ($return) {
			$msg = array (
					'rtn_code' => 0,
					'content' => '删除成功!' 
			);
			echo json_encode ( $msg );
			exit ();
		} else {
			$msg = array (
					'rtn_code' => 0,
					'content' => '系统错误,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
	}
}
?>